/**
 * 
 */
$(function() {
	$("#zkName").focus();
	$("#addZkB")
			.click(
					function() {
						var $title = $("#zkName").val();
						var $shi = $("#zhekou1").val();
						var $ge = $("#zhekou2").val();
						var $zk = parseFloat("0." + $shi + $ge);
						if ($title == null || $title == "" || $zk == 0) {
							alert("不合法的折扣！");
							return;
						} else {
							$
									.ajax({
										url : "zk.do?method=addZk",
										type : "post",
										data : {
											title : $title,
											zk : $zk
										},
										error : function(request) {
											alert("出现问题，最好重启电脑！");
										},
										success : function(data) {
											if (data.status == 200) {
												$("#tBody")
														.append(
																'<tr class="modifyTr"><td class="zktitle" style="width:50%">'
																		+ $title
																		+ '</td><td class="zkz" style="width:25%;margin-left:4px;">'
																		+ $zk
																		+ '</td>'
																		+ '<td style="width:20%;margin-left:4px;"><input type="hidden" class="hiddenId" value="'
																		+ data.id
																		+ '"/><pre onclick="editTr(this)" class="editSpan mySpan" title="修改">    </pre></td>'
																		+ '</tr>');
												resetForm();
											} else {
												alert(data.msg);
											}
										}
									});
						}
					});

	$("#changeZkB").click(
			function() {
				var $title = $("#zkName").val();
				var $shi = $("#zhekou1").val();
				var $ge = $("#zhekou2").val();
				var $zk = parseFloat("0." + $shi + $ge);
				var $zkId = parseInt($("#hiddenZk").val());
				if ($title == null || $title == "" || $zk == 0 || $zkId == null
						|| $zkId == 0) {
					alert("不合法的折扣！");
					return;
				} else {
					$.ajax({
						url : "zk.do?method=changeZk",
						type : "post",
						data : {
							title : $title,
							zk : $zk,
							id : $zkId
						},
						error : function(request) {
							alert("出现问题，最好重启电脑！");
						},
						success : function(data) {
							if (data.status == 200) {
								$(".choosed").find(".zktitle").text($title);
								$(".choosed").find(".zkz").text($zk);
								resetForm();
								showMsg(data.msg, "center");
							} else {
								alert(data.msg);
							}
						}
					});
				}
			});
});

function resetForm() {
	$("#addZkB").show();
	$("#changeZkB").hide();
	$("#zkName").val("");
	$("#zhekou1").val(9);
	$("#zhekou2").val(0);
	$("#hiddenZk").val(0);
	$(".modifyTr").removeClass("choosed");
}

function editTr(t) {
	var $this = $(t);
	$(".modifyTr").removeClass("choosed");
	var zkId = $this.parent().find(".hiddenId").val();
	if (zkId == 0) {
		alert("此类别不可修改！");
		return;
	} else {
		$this.parent().parent().addClass("choosed");
		$("#zkName").val($this.parent().parent().find(".zktitle").text());
		$("#hiddenZk").val(zkId);
		var $zkz = parseInt(parseFloat($this.parent().parent().find(".zkz")
				.text()) * 100);
		var $shi = parseInt($zkz.div(10));
		var $ge = $zkz - $shi * 10;
		$("#zhekou1").val($shi);
		$("#zhekou2").val($ge);
		$("#addZkB").hide();
		$("#changeZkB").show();
	}
}

function accDiv(arg1, arg2) {
	var t1 = 0, t2 = 0, r1, r2;
	try {
		t1 = arg1.toString().split(".")[1].length
	} catch (e) {
	}
	try {
		t2 = arg2.toString().split(".")[1].length
	} catch (e) {
	}
	with (Math) {
		r1 = Number(arg1.toString().replace(".", ""))
		r2 = Number(arg2.toString().replace(".", ""))
		return (r1 / r2) * pow(10, t2 - t1);
	}
}

Number.prototype.div = function(arg) {
	return accDiv(this, arg);
}